package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.model.entity.pick.TenderAllListEntity;
import com.sgb.kjwl.model.entity.pick.TenderAllListNewEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.material.hall.NewPickHallFragment;
import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.DemandForecastListBean;
import com.sgb.zhaocai.entity.DemandForecastListEntity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NewPickHallFragModel extends BaseViewModel {
    private HomeRepertory repertory;
    private NewPickHallFragment fragment;
    private Map map;
    private int pageSize = 10;
    private String longitude;
    private String latitude;
    private boolean isLastPage;

    public NewPickHallFragModel(NewPickHallFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //招标大厅-全部
    public void findTenderingHomeEsPage(int page, String type, String areacode, String city) {
        if (map == null)
            map = new HashMap();
        map.clear();

        if ("0".equals(type)) {
            type = "";
        }
        map.put("type", type);
        map.put("province", areacode);
        map.put("city", city);

        map.put("queryPosition", 3);
        map.put("current", page);
        map.put("limit", pageSize);

        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().queryHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListNewEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListNewEntity>>> data) {
                if (data == null) {
                    fragment.getHallAllData(new ArrayList<>());
                }
                ;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getHallAllData(data.getData().getList());
                }
            }
        });
    }

    //招标大厅-招标公告
    public void tenderBidderList(int page, String typeNo, String areaCode, String cityNo, String moneyNo, String statusNo, String verticalType) {

        if (map == null)
            map = new HashMap();
        map.clear();

        if (!"".equals(areaCode) && areaCode != null)
            map.put("province", areaCode);
        if (!"".equals(cityNo) && cityNo != null)
            map.put("city", cityNo);

        if ("0".equals(typeNo)) {
            typeNo = "";
        }
        map.put("type", typeNo); // 招标类型
        map.put("qualificationWay", verticalType);
        map.put("sortMethod", 1);
        map.put("queryPosition", 4);
        map.put("price", moneyNo);
        map.put("limit", pageSize);
        map.put("current", page);
        map.put("state", statusNo);

        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().queryHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListNewEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListNewEntity>>> data) {
                if (data == null) {
                    fragment.getHallTenderData(new ArrayList<>());
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getHallTenderData(data.getData().getList());
                }
            }
        });
    }

    //竞价
    public void getBiddingList(int page, String type, String areacode, String city, String price,String fromWhere) {
        if (map == null)
            map = new HashMap();
        map.clear();

        map.put("limit", pageSize);
        map.put("current", page);
        if ("0".equals(type)) {
            type = "";
        }
        map.put("type", type);
        map.put("province", areacode);
        map.put("city", city);
        map.put("price", price);

        map.put("name", "");
        map.put("state", "");
        map.put("longitude", "");
        map.put("latitude", "");

        if ("0".equals(fromWhere)) { //0招标大厅
            map.put("queryPosition", "7");
        } else if ("1".equals(fromWhere)) { //1招标公告
            map.put("queryPosition", "9");
        }

        DialogHelper.showProgressDialog2(fragment.getContext(), null, "数据加载中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().queryHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListNewEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListNewEntity>>> data) {
                if (data == null || data.getState() == null) {
                    fragment.getBiddingData(new ArrayList<>());
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null) {
                        fragment.getBiddingData(data.getData().getList());
                    }
                }
            }
        });
    }

    //招标预告    报名开始时间  招标方式
    public void tenderNoticeList(int page, String type, String areacode, String city, String price, String time, String verticalType) {
        if (map == null)
            map = new HashMap();
        map.clear();

        map.put("limit", pageSize);
        map.put("page", page);
        if ("0".equals(type)) {
            type = "";
        }
        map.put("type", type);
        map.put("areacode", areacode);
        map.put("city", city);
        map.put("price", price);
        map.put("verticalType", verticalType);
        map.put("time", time);

        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().tenderNoticeList(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<PreNoticeItemEntity>>> data) {
                if (data == null || data.getState() == null) {
                    fragment.getNoticeData(new ArrayList<>());
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null) {
                        fragment.getNoticeData(data.getData().getList());
                    }
                }
            }
        });
    }

    //招标公告-全部
    public void findNoticeHomeEsPage(int page, String type, String areacode, String city) {
        if (map == null)
            map = new HashMap();
        map.clear();

        if ("0".equals(type)) {
            type = "";
        }
        map.put("type", type);
        map.put("province", areacode);
        map.put("city", city);

        map.put("queryPosition", 5);
        map.put("current", page);
        map.put("limit", pageSize);

        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().queryHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListNewEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListNewEntity>>> data) {
                if (data == null) {
                    fragment.getHallAllData(new ArrayList<>());
                }
                ;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getHallAllData(data.getData().getList());
                }
            }
        });
    }

    //招标公告--招标报名
    public void getFindPreBiddingList(int page, String typeNo, String areaCode, String cityNo, String priceNo, String time, String verticalType) {
        if (map == null)
            map = new HashMap();
        map.clear();

        if (!"".equals(areaCode) && areaCode != null)
            map.put("province", areaCode);
        if (!"".equals(cityNo) && cityNo != null)
            map.put("city", cityNo);

        if ("0".equals(typeNo)) {
            typeNo = "";
        }
        map.put("type", typeNo); // 招标类型
        map.put("applyEndTime", time); // 报名结束时间
        map.put("qualificationWay", verticalType);
        map.put("sortMethod", 1);
        map.put("queryPosition", 6);
        map.put("price", priceNo);
        map.put("limit", pageSize);
        map.put("current", page);

        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().queryHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListNewEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListNewEntity>>> data) {
                if (data == null) {
                    fragment.getHallTenderData(new ArrayList<>());
                }
                ;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getHallTenderData(data.getData().getList());
                }
            }
        });
    }

    public void getProvinceData() {
        APIManager.getInstance().getPickAPI().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setProvinceDatas(data.getData());
                }
            }
        });
    }

    //类型
    public void getFilterTypeData() {
        APIManager.getInstance().getPickAPI().tenderNewTypeList().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setFilterTypeData(data.getData());
                }
            }
        });
    }

    //概算金额
    public void getPriceData() {
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setPriceData(data.getData());
                }
            }
        });
    }

    //报名结束时间
    public void getPickTimes() {
        APIManager.getInstance().getPickAPI().getPickTimes().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setEndTimeData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //招标状态
    public void getStatusData() {
        Map map = new HashMap();
        map.put("areacode", repertory.getAreaCode());
        APIManager.getInstance().getPickAPI().getProjectStatus(map).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {

            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setStatusData(data.getData());
                }
            }
        });
    }

    //招标方式
    public void getTenderNewTypeList() {
        APIManager.getInstance().getPickAPI().getTenderNewTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTenderTypeData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUrlSetting(String id) {
        APIManager.getInstance().getPickAPI().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url, id);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    public void getUrlSettingSj(String id) {
        APIManager.getInstance().getPickAPI().getUrlSetting("tenderdetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    fragment.getUrl(url, id);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 首页-招标大厅-需求预告列表详情页
     * @param id
     * @param modelType
     */
    public void getWebH5(String id, String modelType) {
        longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
        latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
        if (TextUtils.isEmpty(longitude)) {
            longitude = "0";
        }
        if (TextUtils.isEmpty(latitude)) {
            latitude = "0";
        }
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("demandNoticeDetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String webUrl = data.getData().getUrl() + "?id=" + id + "&localLat=" + latitude + "&localLng=" + longitude + "&type=" + modelType;
                    WebViewX5HostActivity.start(fragment.getActivity(), webUrl);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 需求类型
     */
    public void getDemandTypeList() {
        List<PickSelectEntity> packersEntities = new ArrayList<>();
        PickSelectEntity packer = new PickSelectEntity();
        packer.setNo("0");
        packer.setProjectscale("材料采购");
        packersEntities.add(packer);
        PickSelectEntity packer1 = new PickSelectEntity();
        packer1.setNo("1");
        packer1.setProjectscale("机械租赁");
        packersEntities.add(packer1);
        PickSelectEntity packer2 = new PickSelectEntity();
        packer2.setNo("2");
        packer2.setProjectscale("设备租赁");
        packersEntities.add(packer2);
        PickSelectEntity packer3 = new PickSelectEntity();
        packer3.setNo("3");
        packer3.setProjectscale("劳务用工");
        packersEntities.add(packer3);
        PickSelectEntity packer4 = new PickSelectEntity();
        packer4.setNo("4");
        packer4.setProjectscale("分包计划");
        packersEntities.add(packer4);
        fragment.setDemandTypeList(packersEntities);
    }

    /**
     * 工程类型
     */
    public void getProjectTypeList() {
        APIManager.getInstance().getPickAPI().getFindEngineeringTypeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> packersEntities = new ArrayList<>();
                    List<CommonPopEntity> dataTime = data.getData();
                    for (CommonPopEntity commonPopEntity : dataTime) {
                        PickSelectEntity packer = new PickSelectEntity();
                        packer.setNo(commonPopEntity.getNo());
                        packer.setProjectscale(commonPopEntity.getTypeName());
                        packersEntities.add(packer);
                    }
                    fragment.setProjectTypeList(packersEntities);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 发布时间
     */
    public void getReleaseTimeList() {
        APIManager.getInstance().getPickAPI().getfindProjectTimeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> packersEntities = new ArrayList<>();
                    List<CommonPopEntity> dataTime = data.getData();
                    for (CommonPopEntity commonPopEntity : dataTime) {
                        PickSelectEntity packer = new PickSelectEntity();
                        packer.setNo(commonPopEntity.getNo());
                        packer.setProjectscale(commonPopEntity.getTypeName());
                        packersEntities.add(packer);
                    }
                    fragment.setReleaseTimeList(packersEntities);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    public void getMaterialHomeList(String pos, DemandForecastListBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        Call<BaseEntity<ListResponse<List<DemandForecastListEntity>>>> call = null;
        switch (pos) {
            case "0":
                call = BaseApi.getApi().getMaterialHomeList(requestBean);
                break;
            case "1":
                call = BaseApi.getApi().getMechanicalHomeList(requestBean);
                break;
            case "2":
                call = BaseApi.getApi().getEquipmentHomeList(requestBean);
                break;
            case "3":
                call = BaseApi.getApi().getLaborHomeList(requestBean);
                break;
            case "4":
                call = BaseApi.getApi().getSubcontractPlanHomeList(requestBean);
                break;
        }
        call.enqueue(new ICallBack<BaseEntity<ListResponse<List<DemandForecastListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<DemandForecastListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    fragment.setDemandList(data.getData().getList(),isLastPage);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }
    /**
     * 材料类型
     */
    public void getMaterialType() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getGoodsType("").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    fragment.setMaterialType(data.getData());
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    /**
     * 机械类型
     */
    public void getMechanicalType( ) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMachineOutTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    fragment.setMechanicalType(data.getData());
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    /**
     * 设备类型
     */
    public void getEquipmentType( ) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDeviceTypeList("").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    fragment.setEquipmentType(data.getData());
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void getUrlSettingTender(String id) {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "加载中", 0, true, null, false);
        APIManager.getInstance().getPickAPI().getUrlSetting("FrontDeskBidding").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url, id);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }
}
